﻿Imports System.Windows

Namespace Views
    Public Class ViewCorrespondance

        'Générer un évènement pour le View-Model à l'obtention et à la perte du focus
        Public Sub New()

            ' Cet appel est requis par le concepteur.
            InitializeComponent()

        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            'TODO: Add event handler implementation here.
            Window.GetWindow(Me).Close()
        End Sub

        Private Sub ViewCorrespondance_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            Dim VM_Correspondance As ViewModels.ViewModelCorrespondance = CType(Me.DataContext, ViewModels.ViewModelCorrespondance)
            AddHandler TxtValFixe.GotFocus, AddressOf VM_Correspondance.onValeurFixeChampsImporte_GotFocus
            AddHandler TxtValFixe.LostFocus, AddressOf VM_Correspondance.onValeurFixeChampsImporte_LostFocus
        End Sub


        Private Sub TxtValFixe_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TxtValFixe.TextChanged
            'Générer un évènement pour rafraichir le texte
            If Not Me.DataContext Is Nothing Then
                Dim VM_Correspondance As ViewModels.ViewModelCorrespondance = CType(Me.DataContext, ViewModels.ViewModelCorrespondance)
                VM_Correspondance.ValeurFixeChampImporte = TxtValFixe.Text
                'If TxtValFixe.Text = "=""""" Then
                '    TxtValFixe.SelectionStart = 2
                'End If
            End If
        End Sub

        Private _txt_chConstant_DernierePosCurseurValide As Integer = 0
        Private Sub TxtValFixe_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TxtValFixe.SelectionChanged
            If TxtValFixe.Text = "=""""" And TxtValFixe.SelectionStart <> 2 Then
                TxtValFixe.SelectionStart = 2
            End If
            If TxtValFixe.IsFocused Then
                If _txt_chConstant_DernierePosCurseurValide <> TxtValFixe.SelectionStart Then
                    If TxtValFixe.SelectionStart <> 0 Then
                        _txt_chConstant_DernierePosCurseurValide = TxtValFixe.SelectionStart
                    Else
                        TxtValFixe.SelectionStart = _txt_chConstant_DernierePosCurseurValide
                    End If
                End If
            End If
        End Sub

    End Class

End Namespace